<?php
/**
count by where clause
*/
function smarty_function_count_from_db ($params, &$smarty) 
{

    $table = $params['table'];
    $where = $params['where'];
    $order_by = $params['order_by'];
    $group_by = $params['group_by'];
    $fixed      = $params['fixed'];

    $table = DBUtil::getLimitedTablename($table);
    if ($table&&$fixed){
      //$where = DBUtil::_checkWhereClause($where);
      //$order_by =DBUtil::_checkOrderByClause($order_by);
      //$group_by =DBUtilEx::_checkGroupByClause($group_by);
      $where_fixed = "where ".$fixed." = ".$where;
      $sql = "SELECT COUNT(*) FROM $table $where_fixed $group_by $order_by";
      $column = array("count");
      $result = DBUtil::executeSQL($sql);
      $objectArray = DBUtil::marshallObjects ($result, $column);
      $value = $objectArray['0']['count'];
      if(!empty($value)){
        return $value;
      }
      return '0';


    }else if ($table){
      $where = DBUtil::_checkWhereClause($where);
      $order_by =DBUtil::_checkOrderByClause($order_by);
      $group_by =DBUtilEx::_checkGroupByClause($group_by);

      $sql = "SELECT COUNT(*) FROM $table $where $group_by $order_by";

      $column = array("count");
      $result = DBUtil::executeSQL($sql);
      $objectArray = DBUtil::marshallObjects ($result, $column);
      $value = $objectArray['0']['count'];
      if(!empty($value)){
        return $value;
      }
      return '0';
    }

    return true;
}
?>